package hotel.hotel;

import hotel.exception.BuildingsMissingException;
import hotel.exception.NotEnoughPlaceException;

import java.util.HashMap;
import java.util.Map;

public class HotelsArea implements IModifiableHotelsArea {
	
	// ATTRIBUTS
	
	private final IProperty[] properties;
	private final Map<IProperty, Integer> buildings;
	private final Map<IProperty, Boolean> funAreas;
	
	// CONSTRUCTEURS
	
	public HotelsArea(IProperty[] prop) {
		properties = prop.clone();
		buildings = new HashMap<IProperty, Integer>();
		funAreas = new HashMap<IProperty, Boolean>();
	}
	
	// REQUETES

	@Override
	public IProperty[] getProperties() {
		return properties.clone();
	}

	@Override
	public int getBuiltNb(IProperty property) {
		Integer i = buildings.get(property);
		return i == null ? 0 : i;
	}

	@Override
	public boolean hasFunArea(IProperty property) {
		Boolean fun = funAreas.get(property);
		return fun == null ? false : fun;
	}
	
	// COMMANDES

	@Override
	public void build(IProperty property) throws NotEnoughPlaceException {
		int old = getBuiltNb(property);
		if (old == property.getCapacity()) {
			throw new NotEnoughPlaceException();
		}
		buildings.put(property, old + 1);
	}

	@Override
	public void addFunArea(IProperty property) throws BuildingsMissingException, NotEnoughPlaceException {
		if (getBuiltNb(property) < property.getCapacity()) {
			throw new BuildingsMissingException();
		}
		if (hasFunArea(property)) {
			throw new NotEnoughPlaceException();
		}
		funAreas.put(property, true);
	}

}
